//
//  NewsViewController.m
//  Today
//
//  Created by fanfan on 15/5/22.
//  Copyright (c) 2015年 fanfan. All rights reserved.
//

#import "NewsViewController.h"

@interface NewsViewController ()

@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"今日头条";
    activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activity.color = [UIColor grayColor];
    activity.frame = CGRectMake(0, 0, 200, 200);
    activity.center = self.view.center;
    [self.view addSubview:activity];
    [activity startAnimating];
    [self loadNews];
}
- (void)initNewsTableview
{
    UITableView *NewsTableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height ) style:UITableViewStylePlain];
    [self.view addSubview:NewsTableview];
    NewsTableview.delegate = self;
    NewsTableview.dataSource = self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"cell";
    NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[NewsTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str]autorelease];
    }
    NewsModel *model = [self.array objectAtIndex:indexPath.row];
    cell.NewsTitle.text = model.title;
    cell.NewsTime.text = model.updateTime;
    cell.NewsPerson.text = model.commentsall;
    NSURL *url = [NSURL URLWithString:model.thumbnail];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage imageWithData:data];
    cell.NewsImage.image = image;
    
    return cell;
}
- (void)loadNews
{
    NSString *str = @"http://api.iclient.ifeng.com/ClientNews?id=SYLB10,SYDT10&page=1&gv=4.4.2&av=4.4.2&uid=864690024532981&deviceid=864690024532981&proid=ifengnews&os=android_19&df=androidphone&vt=5&screen=1080x1920&publishid=9023";
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"GET";
    [NSURLConnection connectionWithRequest:request delegate:self];
}
#pragma mark NSUrlconnection 的代理方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    //    接收到服务器响应
    self.Newsdata = [NSMutableData data];
}
//收到服务器反悔的数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.Newsdata appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSArray *array = [NSJSONSerialization JSONObjectWithData:self.Newsdata options:NSJSONReadingMutableContainers error:nil];
    self.array = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        NSArray *array1 = [dic objectForKey:@"item"];
        for (NSDictionary *dic1 in array1) {
            NewsModel *model = [NewsModel getNewsModel];
            model.thumbnail = [dic1 objectForKey:@"thumbnail"];
            model.title = [dic1 objectForKey:@"title"];
            model.updateTime = [dic1 objectForKey:@"updateTime"];
            model.commentsUrl = [dic1 objectForKey:@"commentsUrl"];
            model.commentsall = [dic1 objectForKey:@"commentsall"];
            [self.array addObject:model];
            
        }
    }
    [self initNewsTableview];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 110;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsModel *newsmodel = [self.array objectAtIndex:indexPath.row];
    NewsFirstViewController *NewsFirst = [[NewsFirstViewController alloc]init];
    NewsFirst.NewsUrl= newsmodel.commentsUrl;
    [self.navigationController pushViewController:NewsFirst animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
